package com.lv.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lv.aop.Log;
import com.lv.common.AjaxResult;

import com.lv.common.dto.UserList;
import com.lv.entity.Role;
import com.lv.entity.User;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * <p>
 *  用户管理接口
 * </p>
 *
 * @author 凉薄
 * @since 2022-01-06
 */
@RestController
@RequestMapping("/sys/user")
public class UserController extends BaseController {
    @GetMapping("/userInfo")
    public AjaxResult getUserInfo(){
        Long id = tokenUntil.getIdByToken(request);
        User user = userService.getById(id);
        user.setPassword("");

        return AjaxResult.success(user);
    }

    @GetMapping("/getAllUser/{current}/{size}")
    @PreAuthorize("hasAuthority('sys:user:manager')")
    public AjaxResult getAllUser(@PathVariable Integer current,@PathVariable Integer size,String username){
        IPage<UserList> all = userService.getAll(current, size, username);
        return AjaxResult.success(all);
    }
    @Log("删除用户")
    @DeleteMapping("/deleteUser/{id}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public AjaxResult deleteUser(@PathVariable Long id){
        try {
            userService.deleteUser(id);
        } catch (Exception e) {
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }

    @Log("新增用户")
    @PostMapping("/saveUser/{type}")
    public AjaxResult saveUser(@PathVariable Integer type,@RequestBody User user){
        boolean b = userService.save(user);
        if(!b){
            return AjaxResult.error(type==1?"添加失败":"注册失败");
        }
        return AjaxResult.success(type==1?"添加成功":"注册成功");
    }

    @Log("更新用户")
    @PostMapping("/updateUser")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public AjaxResult updateUser(@RequestBody User user){
        boolean b = userService.updateById(user);
        if(!b){
            return AjaxResult.error("更新失败");
        }
        userService.clearAuthoritiesInfo(user.getId());
        return AjaxResult.success("更新成功");
    }

    @GetMapping("/getUserRole/{id}")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public AjaxResult getUserRoleId(@PathVariable Long id){
        Role role = userRoleService.getUserRole(id);
        return AjaxResult.success(role);
    }

    @PostMapping("/updateRole/{userId}/{roleId}")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public AjaxResult updateRole(@PathVariable Long userId,@PathVariable Long roleId){
        try {
            userService.updateRole(userId,roleId);
        } catch (Exception e) {
            return AjaxResult.error("分配失败");
        }
        userService.clearAuthoritiesInfo(userId);
        return AjaxResult.success("分配成功");
    }
    @PostMapping("/replacePass/{id}")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public AjaxResult replacePass(@PathVariable Long id){
        User user=new User();
        user.setId(id);
        user.setPassword(passwordEncoder.encode("888888"));
        boolean b = userService.updateById(user);
        if(!b){
            return AjaxResult.error("重置失败");
        }
        return AjaxResult.success("重置成功");
    }

    @PostMapping("/deleteUsers")
    public AjaxResult deleteUsers(@RequestBody List<User>list){
        boolean b = userService.removeByIds(list);
        if(!b){
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }
}

